import path from "path";
import { defineConfig,loadEnv } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import vwt from "weapp-tailwindcss-webpack-plugin/vite";
import postcssWeappTailwindcssRename from "weapp-tailwindcss-webpack-plugin/postcss";

const isH5 = process.env.UNI_PLATFORM === "h5";
const resolve = (p) => {
  return path.resolve(__dirname, p);
};

const postcssPlugins = [
  require("autoprefixer")(),
  require("tailwindcss")({
    config: resolve("./tailwind.config.js"),
  }),
];
if (!isH5) {
  postcssPlugins.push(postcssWeappTailwindcssRename({}));
}

// https://vitejs.dev/config/
export default defineConfig(({ command, mode })=> {
	//检查process.cwd()路径下.env.development.local、.env.development、.env.local、.env这四个环境文件
	loadEnv(mode, process.cwd());
	return{
	  plugins: [
		  uni(),
		  vwt(),
	  ],
	  resolve: {
	      // 配置别名
	      alias: {
	          '@': resolve('src')
	      }
	    },
	  css: {
		  postcss: {
			// 内联写法
			plugins: postcssPlugins,
		  },
		  preprocessorOptions: {
				scss: {
				  additionalData: `@import './src/uni.scss';`
				}
		  }
		}
	}
});
